package com.lzxuni.modules.enterprise;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.system.service.BaseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  企业信息 前端控制器
 * </p>
 *
 * @author liyl
 * @since 2019-06-27
 */
@RestController
@RequestMapping("/Enterprise")
public class EnterpriseInfoController extends BaseController {

    @Autowired
    private EnterpriseInfoService enterpriseInfoService;

    @Autowired
    private FileEntityService fileEntityService;

    @Autowired
    private BaseAreaService baseAreaService;

  /*  @RequestMapping("/NewEnterprises")
    public ModelAndView index() throws Exception {
        ModelAndView mv = new ModelAndView("/NewEnterprises.html");
        List<EnterpriseInfo> enterpriseList = enterpriseInfoService.list(new QueryWrapper<EnterpriseInfo>().eq("approval_result","通过").orderByDesc("create_time").last("limit 12"));
        List list1 = null;
        for (int i = 0;i < enterpriseList.size();i ++){
            FileEntity fileEntity = new FileEntity();
            String id = enterpriseList.get(i).getYwId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("demopic");
            list1 = fileEntityService.queryListByFileEntity(fileEntity);
            enterpriseList.get(i).setFileEntities(list1);
        }
        mv.addObject("enterpriseList",enterpriseList);
        return mv;
    }*/

    //企业详细信息
    @RequestMapping(value = "/NewEnterprises_detail/{ywId}",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView EnterpriseDetail(@PathVariable String ywId) throws Exception {
        ModelAndView mv = new ModelAndView("/NewEnterprises_xx");
        System.out.println("id =" + ywId);
        EnterpriseInfo info = enterpriseInfoService.getById(ywId);
        System.out.println("企业信息：" + info.toString());
        if(info.getProvinceId() != null && info.getProvinceId().length() != 0){
            System.out.println("省：" + baseAreaService.getById(info.getProvinceId()).getAreaName());
            mv.addObject("provinceId",baseAreaService.getById(info.getProvinceId()).getAreaName());
        }
        if(info.getCityId() != null && info.getCityId().length() != 0){
            System.out.println("市：" + baseAreaService.getById(info.getCityId()).getAreaName());
            mv.addObject("cityId",baseAreaService.getById(info.getCityId()).getAreaName());
        }
        if(info.getCountyId() != null && info.getCountyId().length() != 0){
            System.out.println("区：" + baseAreaService.getById(info.getCountyId()).getAreaName());
            mv.addObject("countyId",baseAreaService.getById(info.getCountyId()).getAreaName());
        }
        if(info.getTownId() != null && info.getTownId().length() != 0){
            System.out.println("镇：" + baseAreaService.getById(info.getTownId()).getAreaName());
            mv.addObject("townId",baseAreaService.getById(info.getTownId()).getAreaName());
        }
        if(info.getVillageId() != null && info.getVillageId().length() != 0){
            System.out.println("村：" + baseAreaService.getById(info.getVillageId()).getAreaName());
            mv.addObject("villageId",baseAreaService.getById(info.getVillageId()).getAreaName());
        }
        //System.out.println(baseArea.toString());
        FileEntity fileEntity = new FileEntity();
        String newywId = info.getYwId();
        fileEntity.setYwId(newywId);
        fileEntity.setYwType("demopic");
        List<FileEntity> list = fileEntityService.queryListByFileEntity(fileEntity);
        //model.addAttribute("enterpriseInfo",enterpriseInfo);
        //model.addAttribute("list",list);
        info.setFileEntities(list);
        mv.addObject("enterpriseInfo",info);
        return mv;
    }
    //新增企业
//    @RequestMapping("/NewExposureStage.html")
    @RequestMapping("/NewEnterprises")
    public ModelAndView index() throws Exception {
        ModelAndView mv = new ModelAndView("/NewEnterprises.html");
        //int count = informationPublishPressReleaseService.count(new QueryWrapper<InformationPublishPressRelease>().eq("news_type", newsType));
        int count = enterpriseInfoService.count(new QueryWrapper<EnterpriseInfo>().eq("approval_result","通过"));
        //List<EnterpriseInfo> enterpriseList = enterpriseInfoService.list(new QueryWrapper<EnterpriseInfo>().eq("approval_result","通过").orderByDesc("create_time").last("limit 12"));
        mv.addObject("count", count);
        return mv;
    }

    //新增企业详细页
    /*@RequestMapping(value = "/NewExposureStage_details/{newsId}",method = RequestMethod.GET)
    public ModelAndView newexposureStagedetails(@PathVariable String newsId) throws Exception {
        ModelAndView mv = new ModelAndView("/NewExposureStage_xx");
        List<InformationPublishPressRelease> bgtList = informationPublishPressReleaseService.list(new QueryWrapper<InformationPublishPressRelease>().eq("news_id",newsId));
        mv.addObject("bgtList", bgtList);
        return mv;
    }*/
    //按审核通过列表分页
    @RequestMapping("/listDoType.html")
    public Object typelistDo(String pagination) throws Exception {
        PageParameter pageParameter = JSON.parseObject(pagination.replaceAll("&＃39;", "\""), PageParameter.class);
        //PageData pageData = getPageData(informationPublishPressReleaseService.queryPagebm(pageParameter,newsType));
        PageData pageData = getPageData(enterpriseInfoService.queryPagebm(pageParameter));
        System.out.println("页：" + pageData.toString());
        return pageData;
    }

}

